package web.blogsphere.controller;

import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import web.blogsphere.constant.Constants;
import web.blogsphere.model.BlogInfo;
import web.blogsphere.service.BlogService;
import web.blogsphere.utils.JwtUtils;

import java.util.List;

/**
 * @ClassName BlogController
 * @Description TODO
 * @Author ZJX
 * @Date 2024/9/3 22:39
 * @Version 1.0
 */
@Slf4j
@RequestMapping("/blog")
@RestController
public class BlogController {

    @Autowired
    private BlogService blogService;

    @RequestMapping("/getList")
    public List<BlogInfo> queryBlogList(){
        log.info("获取博客列表...");
        return blogService.queryBlogList();
    }

    @RequestMapping("/getBlogDetail")
    public BlogInfo getBlogDetail(Integer blogId,HttpServletRequest request){
        log.info("getBlogDetail, blogId: {}", blogId);
        BlogInfo blogDetail = blogService.getBlogDetail(blogId);
        //获取token
        String token = request.getHeader(Constants.REQUEST_HEADER_TOKEN);
        //从token中获取登录用户ID
        Integer userId = JwtUtils.getIdByToken(token);
        //判断作者是否为登录用户
        if (userId != null && userId.equals(blogDetail.getUserId())){
            blogDetail.setLoginUser(true);
        }else {
            blogDetail.setLoginUser(false);
        }
        return blogDetail;
    }

    @RequestMapping("/add")
    public Boolean addBook(@RequestBody BlogInfo blogInfo, HttpServletRequest request){
        log.info("添加图书, blogInfo: {}", blogInfo);
        //参数校验
        if (!StringUtils.hasLength(blogInfo.getTitle()) || !StringUtils.hasLength(blogInfo.getContent())){
            return false;
        }
        //获取登录用户
        //获取token
        String token = request.getHeader(Constants.REQUEST_HEADER_TOKEN);
        //从token中获取登录用户ID
        Integer userId = JwtUtils.getIdByToken(token);
        if (userId == null || userId <= 0){
            return false;
        }
        blogInfo.setUserId(userId);
      return  blogService.insertBook(blogInfo);

    }

    @RequestMapping("/update")
    public Boolean update(BlogInfo blogInfo){
        log.info("更新博客, blogInfo:{}",blogInfo);
        //参数校验
        if (blogInfo.getId() == null
                || !StringUtils.hasLength(blogInfo.getTitle())
                || !StringUtils.hasLength(blogInfo.getContent())){
            return false;
        }
        blogService.update(blogInfo);
        return true;
    }

    @RequestMapping("/delete")
    public Boolean delete(Integer blogId){
        log.info("删除博客, blogId:{}", blogId);
        if (blogId <= 0){
            return false;
        }
        BlogInfo blogInfo = new BlogInfo();
        blogInfo.setId(blogId);
        blogInfo.setDeleteFlag(1); //删除
        blogService.update(blogInfo);
        return true;
    }
}
